package com.ruoyi.framework.service;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DeleteFlag;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.RedisUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;

import static com.ruoyi.common.constant.CacheConstants.SMS_CODE_KEY;

/**
 * @author liutao
 * @version 1.0
 * @description: TODO
 * @date 2024/3/13 01:11
 */
@Slf4j
@Service
public class SmsService {

    @Resource
    private ISysUserService userService;

    /**
     *
     * @param phone
     * @param type
     * @return
     */
    public String getCacheKey(String phone, Integer type) {
        return StrUtil.format(SMS_CODE_KEY, type, phone);
    }

    /**
     * 发送短信
     * @param phone 手机号码
     * @param type 短信类型 0-登录验证码；1-注册验证码；2-修改密码/重置密码
     * @return
     */
    public String sendSms(String phone, Integer type) {
        if(type == null){
            throw new ServiceException("请输入短信类型");
        }
        String cacheKey = getCacheKey(phone, type);

        boolean sendFlag = false;
        String code = StringUtils.getRandCode(6);
        if(type == 0 || type == 2){
            sendFlag = true;
            SysUser user = userService.selectUserByPhone(phone);
            if (StringUtils.isNull(user)) {
                log.info("用户：{} 不存在.", phone);
                throw new ServiceException("登录用户：" + phone + " 不存在");
            } else if (DeleteFlag.DELETE.getCode().equals(user.getDelFlag())) {
                log.info("用户：{} 已被删除.", phone);
                throw new ServiceException("对不起，您的账号：" + phone + " 已被删除");
            } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
                log.info("用户：{} 已被停用.", phone);
                throw new ServiceException("对不起，您的账号：" + phone + " 已停用");
            } else if (UserStatus.AUDIT.getCode().equals(user.getStatus())) {
                log.info("用户：{} 处于待审核，请联系管理员.", phone);
                throw new ServiceException("对不起，您的账号：" + phone + " 处于待审核，请联系管理员");
            } else if (UserStatus.REJECT.getCode().equals(user.getStatus())) {
                log.info("用户：{} 您的账号未审核通过，请联系管理员.", phone);
                throw new ServiceException("对不起，您的账号：" + phone + " 您的账号未审核通过，请联系管理员");
            }
        } else if(type == 1){
            sendFlag = true;
        } else {
            sendFlag = false;
        }

        if(sendFlag) {
            log.info("手机校验码：" + code);
            RedisUtils.setCacheObject(cacheKey, code, Duration.ofMinutes(3));
            return code;
        }else{
            throw new ServiceException("验证码发送失败！");
        }
    }

    /**
     * 验证验证码
     * @param phone
     * @param type
     * @param code
     */
    public void checkCode(String phone, Integer type, String code) {
        if(type == null){
            throw new ServiceException("请输入短信类型");
        }
        if(StrUtil.isEmpty(code)){
            throw new ServiceException("验证码不能为空");
        }
        if(StrUtil.isEmpty(phone)){
            throw new ServiceException("手机号码不能为空");
        }
        String cacheKey = getCacheKey(phone, type);
        String cacheCode = RedisUtils.getCacheObject(cacheKey);
        if (!StrUtil.equals(cacheCode, code)) {
            throw new ServiceException("验证码错误或已过期");
        }
        RedisUtils.deleteObject(cacheKey);
    }
}
